package com.aitemv.web.upload;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.aitemv.web.base.BaseAction;
@Controller
public class FileUploadAction extends BaseAction implements ServletContextAware{
	private ServletContext servletContext;
	
	@RequestMapping("upload.htm")
	public void uploadImg(MultipartHttpServletRequest request,HttpServletResponse response) throws IOException{
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		SimpleDateFormat simpleFormat = new SimpleDateFormat("MMddHHmmsss");
		String generationfileName = simpleFormat.format(new Date())+new Random().nextInt(1000);
		//保存路径
		String ss = request.getContextPath();
		String tt = request.getServletPath();
		String savePath = this.servletContext.getRealPath("/uploadImage");
		String updateP = request.getParameter("ufile");
		String fileNameSuffix=null;
		String fileName= null;
		if(null!=updateP&&!"".equals(updateP)){
			try {
				MultipartFile mf = request.getFile(updateP);
				fileName=mf.getOriginalFilename();
				if(null!=mf&&!"".equals(mf)){
					fileNameSuffix=fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
					SaveFileFromInputStream(mf.getInputStream(),savePath,generationfileName+"."+fileNameSuffix);
					out.write("{'state':'0','fileName':'/uploadImage/"+generationfileName+"."+fileNameSuffix+"','info':'suc'}");
				}
			} catch (Exception e) {
				out.write("{'state':'1'}");
				e.printStackTrace();
			}
		}	
	}
//保存文件   1,文件   2，保存路径 3，文件名称
	public void SaveFileFromInputStream(InputStream stream,String path,String filename) throws IOException{      
      	  FileOutputStream fs=new FileOutputStream( path + "/"+ filename);
      	  byte[] buffer =new byte[1024*1024];
      	  int bytesum = 0;
      	  int byteread = 0; 
      	  while ((byteread=stream.read(buffer))!=-1){
      	     bytesum+=byteread;
        	   fs.write(buffer,0,byteread);
     	      fs.flush();
       	 } 
       	 fs.close();
        	stream.close();      
	}
	@Override
	public String forward(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		return null;
	}
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}
	
	
}
